<?php
    /**
 * Course selector is a page that allows a user to select a course
 *
 * @Author Ryan Olson
 * @version 1.0
 */

@session_start();
require_once 'shared-functions.php';
require_once 'session.php';
require_once 'masterpage.php';

//Make sure the student is logged in
if (!IsValidSession()) {
    header('Location: login.php?page=course-selector');
    exit();
}
else
{
    RefreshSession();
}


//If this page has been sent a page to redirect to, so it can use the selected course for other purposes, then save that destination
if (!isset($_GET['page'])) {
    //do something if they didn't pass a page
    header('Location: index.php');
}
else
{
    $destination = $_GET['page'] . ".php";
}

//Print out the master page
masterpage("Student Courses");
?>
<form action="course-selector.php?page=<?php    echo $_GET['page'];       //Generate page details for redirect
if (isset($_GET['filter'])) {
    echo '&filter=' . $_GET['filter'];
}

?>" method="post">
<?php
    if (!isset($_POST['SemesterID'])) {
    echo 'Semester: <select name="semester">';
    $selected = "";
    if (!isset($_POST['semester'])) {
        $selected = ' selected="true"';
    }
    echo '<option value="-1"' . $selected . '>All</option>';
    $link = connect_db();
    //query to get all the semesters
    $query = "SELECT * FROM `Semester`";
    $results = mysql_query($query, $link);
    while ($result = mysql_fetch_array($results, MYSQL_BOTH))
    {
        $selected = "";
        if (isset($_POST['semester']) && $_POST['semester'] == $result[2]) {
            $selected = ' selected="true"';
        }
        echo '<option value="' . $result[2] . '"' . $selected . '>' . $result[3] . '</option>';
    }
    echo '</select>';
    echo '<br />';
}
    ?>
    <input type="text" name="txtQuery" id="txtQuery" size="25"/>
    <input type="submit" value="Search"/>
<?php
            if (isset($_POST['StudentID'])) {
    echo '<input type="hidden" name="StudentID" value="' . $_POST['StudentID'] . '" />';
}
    if (isset($_POST['SemesterID'])) {
        echo '<input type="hidden" name="SemesterID" value="' . $_POST['SemesterID'] . '" />';
    }
    ?>
</form>
<p/>
<?php
    $link = connect_db();

//Query to receive an array of the Student's current courses
if (isset($_GET['filter']) && $_GET['filter'] == 'student') {

    //Query to get students courses
    $query = "SELECT `Course`.`Name`, `Course`.`Credits`, `Instructor`.`FirstName`, `Instructor`.`MiddleName`, `Instructor`.`LastName`,
                        `CourseInstance`.`DaysTaught`, `CourseInstance`.`SemesterID`, `CourseInstance`.`InstanceID`, `Semester`.`Description`
		    FROM `User`
		    INNER JOIN `Student` ON `Student`.`ID` = `User`.`UserId`
		    INNER JOIN `CourseEnrollment`ON `CourseEnrollment`.`StudentID` = `Student`.`ID`
		    INNER JOIN `CourseInstance` ON `CourseInstance`.`InstanceID` = `CourseEnrollment`.`CourseInstanceID`
		    INNER JOIN `Semester` ON `Semester`.`semesterID` = `CourseInstance`.`semesterID`
		    INNER JOIN `User` AS `Instructor` ON `Instructor`.`UserID` = `CourseInstance`.`InstructorID`
		    INNER JOIN `Course` ON `Course`.`CourseID` = `CourseInstance`.`CourseID`
		    WHERE `User`.`UserID` = '" . GetSessionUser() . "'";
}
else if (isset($_GET['filter']) && $_GET['filter'] == 'teacher') {
    //Query to get teachers courses
    $query = "SELECT `Course`.`Name`, `Course`.`Credits`, `Instructor`.`FirstName`, `Instructor`.`MiddleName`, `Instructor`.`LastName`,
                        `CourseInstance`.`DaysTaught`, `CourseInstance`.`SemesterID`, `CourseInstance`.`InstanceID`, `Semester`.`Description`
		    FROM `User`
		    INNER JOIN `CourseInstance` ON `CourseInstance`.`InstructorID` = `User`.`UserID`
		    INNER JOIN `Semester` ON `Semester`.`semesterID` = `CourseInstance`.`semesterID`
		    INNER JOIN `User` AS `Instructor` ON `Instructor`.`UserID` = `CourseInstance`.`InstructorID`
		    INNER JOIN `Course` ON `Course`.`CourseID` = `CourseInstance`.`CourseID`
		    WHERE `User`.`UserID` = '" . GetSessionUser() . "'";
}
else
{
    //Query to get all courses
    $query = "SELECT `Course`.`Name`, `Course`.`Credits`, `Instructor`.`FirstName`, `Instructor`.`MiddleName`, `Instructor`.`LastName`,
                        `CourseInstance`.`DaysTaught`, `CourseInstance`.`SemesterID`, `CourseInstance`.`InstanceID`, `Semester`.`Description`
		    FROM `CourseInstance`
		    INNER JOIN `Semester` ON `Semester`.`semesterID` = `CourseInstance`.`semesterID`
		    INNER JOIN `Course` ON `Course`.`CourseID` = `CourseInstance`.`CourseID`
		    INNER JOIN `User` AS `Instructor` ON `Instructor`.`UserID` = `CourseInstance`.`InstructorID`";
    if (isset($_POST['txtQuery'])) {
        $query .= " WHERE `Course`.`CourseID` LIKE '%%'";
    }
}

//If the user is performing a search
if (isset($_POST['txtQuery'])) {
    $query .= " AND (`CourseInstance`.`InstanceID` LIKE '%" . $_POST['txtQuery'] . "%'";
//    if(isset($_GET['filter']) && ($_GET['filter'] == 'student' || $_GET['filter'] == 'student'))
//    {
        $query .= " OR `Instructor`.`FirstName` LIKE '%" . $_POST['txtQuery'] . "%'";
        $query .= " OR `Instructor`.`MiddleName` LIKE '%" . $_POST['txtQuery'] . "%'";
        $query .= " OR `Instructor`.`LastName` LIKE '%" . $_POST['txtQuery'] . "%'";
//    }
//    else
//    {
//        $query .= " OR `FirstName` LIKE '%" . $_POST['txtQuery'] . "%'";
//        $query .= " OR `MiddleName` LIKE '%" . $_POST['txtQuery'] . "%'";
//        $query .= " OR `LastName` LIKE '%" . $_POST['txtQuery'] . "%'";
//    }
    $query .= " OR `DaysTaught` LIKE '%" . $_POST['txtQuery'] . "%'";
    $query .= " OR `Course`.`Name` LIKE '%" . $_POST['txtQuery'] . "%'";
    $query .= " OR `Course`.`Credits` LIKE '%" . $_POST['txtQuery'] . "%'";
    $query .= " OR `Semester`.`Description` LIKE '%" . $_POST['txtQuery'] . "%')";
    if (isset($_POST['semester']) && $_POST['semester'] != -1) {
        $query .= " AND `CourseInstance`.`SemesterID` = '" . $_POST['semester'] . "'";
    }
    if (isset($_POST['SemesterID'])) {
        $query .= " AND `CourseInstance`.`SemesterID` = '" . $_POST['SemesterID'] . "'";
    }
}

if (isset($_POST['SemesterID']) && (!isset($_POST['txtQuery']))) {
    $query .= " AND `CourseInstance`.`SemesterID` = '" . $_POST['SemesterID'] . "'";
}

$courses = mysql_query($query, $link);

echo "<table class='DataList'>";
//echo "  <thead>";
echo '	 <tr><th class="header" colspan="6">Select a Course</th></tr>';
echo "   <tr>
                <th class='header'></th>
                <th class='header'>Name</th>
                <th class='header'>Credits</th>
                <th class='header'>Instructor</th>
                <th class='header'>Days Taught</th>
                <th class='header'>Semester</th>
         </tr>";
//echo "  </thead>";
//echo "  <tbody>";

//If there is an error in the query
if (!$courses) {
    echo '<tr><td class="rows" colspan="3">Oops...something went wrong. Please contact support.' . mysql_error() . '</td></tr>';
    exit();
}

//If there are no courses returned
if (mysql_num_rows($courses) < 1) {
    echo '<tr><td class="rows" colspan="6">No courses found.</td></tr>';
    exit();
}

$rowID = 0;
while ($course = mysql_fetch_array($courses, MYSQL_BOTH))
{

    if ($rowID == 0) {
        $rowClass = "rows";
        $rowID++;
    }
    else
    {
        $rowClass = "altrows";
        $rowID--;
    }

    echo '
	    <form action="' . $destination . '" method="post">
	        <input type="hidden" name="CourseInstance" value="' . $course['InstanceID'] . '" />';
    if (isset($_GET['id'])) {
        echo '<input type="hidden" name="StudentID" value="' . $_GET['id'] . '" />';
    }
    if (isset($_POST['StudentID'])) {
        echo '<input type="hidden" name="StudentID" value="' . $_POST['StudentID'] . '" />';
    }
    if (isset($_POST['SemesterID'])) {
        echo '<input type="hidden" name="SemesterID" value="' . $_POST['SemesterID'] . '" />';
    }
    echo '
	        <tr>
	            <td class="' . $rowClass . '"><input type="submit" value="Select" /></td>
	            <td class="' . $rowClass . '">' . $course['Name'] . '</td>
	            <td class="' . $rowClass . '">' . $course['Credits'] . '</td>
	            <td class="' . $rowClass . '">' . $course['LastName'] . ', ' . $course['FirstName'] . ' ' . $course['MiddleName'] . '</td>
	            <td class="' . $rowClass . '" width="200">' . $course['DaysTaught'] . '</td>
	            <td class="' . $rowClass . '">' . $course['Description'] . '</td>
            </tr>
	</form>';
}

//echo '</tbody>';
echo '</table>';

endmasterpage();
?>
